from bs4 import BeautifulSoup
import requests
import time


def get_info(url):
    ret = requests.get(url)
    soup = BeautifulSoup(ret.text)
    r = soup.find_all('div', class_ = 'row-m')
    # 下一页链接
    pages= soup.find('div', class_='pages clear').find_all('a')
    next_page = pages[-1]['href']

    s = ''
    for item in r:
        s += ','.join(item.get_text().strip().split('\n')) + '\n'
    return s, next_page


ret = requests.get("https://www.wood888.net/caigou/")

soup = BeautifulSoup(ret.text)

r = soup.find_all('li',class_='fd-clr')

for i in r:
    for a in i.find_all('a'):
        print(a['href'])
    

l = get_info('https://www.wood888.net/caigou/hongmucaigouxinxi-3.html')
print(l)


if __name__ == "__main__":

    with open('wood188.txt','w') as f:
        ret = requests.get("https://www.wood888.net/caigou/")
        soup = BeautifulSoup(ret.text)
        r = soup.find_all('li',class_='fd-clr')
        for i in r:
            for a in i.find_all('a'):
                s , next_page = get_info(a['href'])
                f.write(s)
                while next_page != a['href']:
                    s, next_page = get_info(next_page)
                    f.write(s)
                    time.sleep(10)
                f.write('\n\n')

